<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1.0" /> <!--自适应-->
		<link rel="stylesheet" type="text/css" href="css/linaliankan.css" />
		
		<script>
			
			var selectNum=0;//标记点击的次数
			var x1,y1,imgNum1,p1,p2;
			
			var board=new Array();
			for (var i=0;i<8;i++) {
				board[i]=new Array();
				for (var j=0;j<8;j+=2){
					board[i][j]=board[i][j+1]=Math.floor(Math.random()*9)+1;
				}
				
			}
			console.log(board);
			
			
			for (var i=1;i<board.length*board.length*100;i++) {
				var x1=Math.floor(Math.random()*board.length);
				var y1=Math.floor(Math.random()*board[0].length);
				var x2=Math.floor(Math.random()*board.length);
				var y2=Math.floor(Math.random()*board[0].length);
				
				var t=board[x1][y1];
				board[x1][y1]=board[x2][y2];
				board[x2][y2]=t;
			}
			
			document.write("<div id='body'>");
			document.write("<div id='game'>");
			for (var i=0;i<board.length;i++) {
				var arr=board[i];
				for (var j=0;j<board[i].length;j++){
					var picid =board[i][j];
					document.write("<img src='img/"+picid+".jpg' onclick='select("+i+","+j+")' id='pic"+i+j+"' />");
				}
				document.write("<br />")
			}
			
			
			function isOneLine(p1,p2){
				var x11=p1.charAt(3);
				var y11=p1.charAt(4);
				var x22=p2.charAt(3);
				var y22=p2.charAt(4);
				//console.log(x11+y11+x22+y22)
				if(x11>x22){
					p1="pic"+x22+y22;
					p2="pic"+x11+y11;
				}else if(y11>y22){
					p1="pic"+x22+y22;
					p2="pic"+x11+y11;
				}else{
					p1="pic"+x11+y11;
					p2="pic"+x22+y22;
				}
				x11=p1.charAt(3);
				y11=p1.charAt(4);
				x22=p2.charAt(3);
				y22=p2.charAt(4);
				//一条直线--》同排 同列
				if(x11==x22 && y11!=y22){//同排
					//console.log(eval(y11+"+"+"1"));
					//console.log(typeof(eval(y11+"+"+"1")));
					//console.log(eval(y22));
					//console.log(typeof(eval(y22)));
					for(var i=eval(y11+"+"+"1");i<eval(y22);i++){
						//console.log(board[x11][i]);
						//console.log(typeof(document.getElementById("pic"+x11+i).className));
						//console.log(document.getElementById("pic"+x11+i).className!='delect');
						if(document.getElementById("pic"+x11+i).className!='delect'){//表示0折此路不通
							return false;
						}
					}
					return true;
				}else if(y11==y22 && x11!=x22){//同列
					for(var i=eval(x11+"+"+"1");i<eval(x22);i++){
						//console.log(board[i][y11]);
						//console.log(typeof(document.getElementById("pic"+i+y11).className));
						//console.log(document.getElementById("pic"+i+y11).className!='delect');
						if(document.getElementById("pic"+i+y11).className!='delect'){//表示0折此路不通
							return false;
						}
					}
					return true;
				}
				return false;
			}
			
			
			function isOneConner(p1,p2){
				//不同排 不同列才能一折
				var x11=p1.charAt(3);
				var y11=p1.charAt(4);
				var x22=p2.charAt(3);
				var y22=p2.charAt(4);
				var left = "pic"+x11+y22;
				var right = "pic"+x22+y11;
				console.log(document.getElementById("pic"+x11+y22).className=='delect');
				console.log(isOneLine(p1,left));
				console.log(isOneLine(p2,left));
				console.log(document.getElementById("pic"+x22+y11).className=='delect');
				console.log(isOneLine(p1,right));
				console.log(isOneLine(p2,right));
				if(document.getElementById("pic"+x11+y22).className=='delect'&&isOneLine(p1,left)&&isOneLine(p2,left) || document.getElementById("pic"+x22+y11).className=='delect'&&isOneLine(p1,right)&&isOneLine(p2,right)){
					return true;
				}
				return false;
			}
			
			function isHorizontal(p1,p2){
				var x11=p1.charAt(3);
				var y11=p1.charAt(4);
				var x22=p2.charAt(3);
				var y22=p2.charAt(4);
				for(var i=0;i<board.length;i++){
					var left = "pic"+i+y11;
					var right = "pic"+i+y22;
					if(document.getElementById("pic"+i+y11).className=='delect'&&isOneLine(left,p1)&&isOneConner(left,p2) || document.getElementById("pic"+i+y22).className=='delect'&&isOneLine(p1,right)&&isOneConner(p2,right)){
						return true;
					}
				}
				return false;
			}
			
			function isVertical(p1,p2){
				var x11=p1.charAt(3);
				var y11=p1.charAt(4);
				var x22=p2.charAt(3);
				var y22=p2.charAt(4);
				for(var i=0;i<board[x11].length;i++){
					var left = "pic"+i+y11;
					var right = "pic"+i+y22;
					if(document.getElementById("pic"+x11+i).className=='delect'==0&&isOneLine(left,p1)&&isOneConner(left,p2) || document.getElementById("pic"+x22+i).className=='delect'&&isOneLine(p1,right)&&isOneConner(p2,right)){
						return true;
					}
				}
				return false;
			}
			
			
			function isTowConner(p1,p2){
				var x11=p1.charAt(3);
				var y11=p1.charAt(4);
				var x22=p2.charAt(3);
				var y22=p2.charAt(4);
				
				if(x11==x22&&(x11==0 || x11==board[x11].length-1)){
					return true;
				}
				if(y11==y22&&(y11==0 || y11==board.length-1)){
					return true;
				}
				if(x11==x22){
					return isHorizontal(p1,p2);
				}
				if(y11==y22){
					return isVertical(p1,p2);
				}
				var flag = isHorizontal(p1,p2);
				if(flag==false){
					return isVertical(p1,p2);
				}
				return flag;
			}
			
			
			
			
			
			
			
			function select(i,j){
				var img = encodeURI(document.getElementById("pic"+i+j).src);
				var index=encodeURI(document.getElementById("pic"+i+j).src).indexOf("img");//71
				//console.log(img)
				//console.log(index);
				//console.log(img.substring(index));
				var imgNum=img.substring(index).charAt(4);//获取图片信息，判断是不是空
				
				if(imgNum==0){//输入已经没有图片的位置
					console.log("空");
					return ;
				}
				document.getElementById("pic"+i+j).className='selectedPic';
				selectNum++;
				
				if(selectNum==1){//保存第一次点击的信息
					x1=i;
					y1=j;
					imgNum1=imgNum;
				}
				if(selectNum==2){
					console.log("点击了两次");
					
					if(x1==i && y1==j){//两次点击的是同一图片
						document.getElementById("pic"+x1+y1).className='unselectedPic';
						document.getElementById("pic"+i+j).className='unselectedPic';
						selectNum=0;
						return ;
					}
					if(imgNum!=imgNum1){//图片不同
						document.getElementById("pic"+x1+y1).className='unselectedPic';
						document.getElementById("pic"+i+j).className='unselectedPic';
						selectNum=0;
						return ;
					}
					if(x1>i){
						p1="pic"+i+j;
						p2="pic"+x1+y1;
					}else if(y1>j){
						p1="pic"+i+j;
						p2="pic"+x1+y1;
					}else{
						p1="pic"+x1+y1;
						p2="pic"+i+j;
					}
					
					if(isOneLine(p1,p2) || isOneConner(p1,p2) || isTowConner(p1,p2)){//同一排  0折+2折
						//成功
						document.getElementById("pic"+x1+y1).className='delect';
						document.getElementById("pic"+i+j).className='delect';
						document.getElementById("pic"+x1+y1).src='img/0.png' ;
						document.getElementById("pic"+i+j).src='img/0.png' ;
						selectNum=0;
					}else{
						//失败
						document.getElementById("pic"+x1+y1).className='unselectedPic';
						document.getElementById("pic"+i+j).className='unselectedPic';
						selectNum=0;
					}
				}
			}
			
			document.write("</div>");
			document.write("<div id='option'>");
			document.write("</div>");
			document.write("</div>");
		</script>
	</head>
	<body>
	</body>
</html>
